from appium import webdriver
from appium.webdriver.common.appiumby import By
from time import sleep
from appium.webdriver.common.touch_action import TouchAction

desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '7'
desired_caps['deviceName'] = '127.0.0.1:62001'
desired_caps['app'] = r'E:\test\apk\mymoney.apk'
desired_caps['appPackage'] = r'com.mymoney'
desired_caps['appActivity'] = r'com.mymoney.biz.splash.SplashScreenActivity'
desired_caps['noReset'] = False  # False代表重置（重新安装），默认值就是False

driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
driver.implicitly_wait(10)  # 隐式等待
sleep(3)
driver.find_element(By.ID, 'com.mymoney:id/sui_dialog_positive_btn_style1').click()  # 点击同意
sleep(6)
driver.find_element(By.ID, 'com.mymoney:id/select_template_btn').click()  # 进入账本
driver.find_element(By.ID, 'com.mymoney:id/ok_btn').click()  # 选好了
sleep(6)
driver.find_element(By.ID, 'com.mymoney:id/account_centre_icon_default_iv').click()  # 我的
sleep(2)
y1 = driver.get_window_size()['height'] * 0.3
x1 = driver.get_window_size()['width'] * 0.5
y2 = driver.get_window_size()['height'] * 0.7
driver.swipe(x1, y2, x1, y1)
# 设置
driver.find_element(By.XPATH, '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/androidx.viewpager.widget.ViewPager/androidx.recyclerview.widget.RecyclerView/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.ScrollView/android.widget.LinearLayout/androidx.recyclerview.widget.RecyclerView/android.widget.FrameLayout[9]/android.widget.LinearLayout/android.widget.TextView').click()
# 密码保护
driver.find_element(By.ID,'com.mymoney:id/setting_right_tip').click()
sleep(3)
# 密码点击
driver.find_element(By.ID, 'com.mymoney:id/right_switch').click()
sleep(3)
# 手势密码
driver.find_element(By.XPATH,
                    '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.LinearLayout[2]/android.widget.LinearLayout[1]/android.widget.TextView').click()
sleep(3)
# 输入密码
for i in range(2):
    TouchAction(driver).press(x=211, y=287).wait(1000).move_to(x=507, y=287).wait(1000).move_to(x=507, y=583).wait(
        1000).release().perform()
# 输入邮箱
driver.find_element(By.ID, 'com.mymoney:id/email_eactv').send_keys('465317680@qq.com')
# 确认邮箱
driver.find_element(By.ID, 'com.mymoney:id/email_again_eactv').send_keys('465317680@qq.com')
# 点击确定
driver.find_element(By.ID, 'com.mymoney:id/ok_btn').click()
sleep(3)
driver.quit()

# 练习：完成随手记的手势密码设置
